'''Simple data loader module.

Loads data files from the "data" directory shipped with a game.

Enhancing this to handle caching etc. is left as an exercise for the reader.
'''

import os
import pyglet
from pyglet.gl import *

data_py = os.path.abspath(os.path.dirname(__file__))
data_dir = os.path.normpath(os.path.join(data_py, '..', 'data'))

def filepath(filename):
    '''Determine the path to a file in the data directory.
    '''
    return os.path.join(data_dir, filename)

def load(filename, mode='rb'):
    '''Open a file in the data directory.

    "mode" is passed as the second arg to open().
    '''
    return open(os.path.join(data_dir, filename), mode)

map_image = None
hero_image = None
e1_image = None
e2_image = None
e3_image = None
smoke_image = None
pickup_image = None
powerup_image = None
rect_image = None

bgrnd_sound = None
pick_sound = None
crash_sound = None

def getTexture(objtype):
    global map_image, hero_image, e1_image, e2_image, smoke_image, pickup_image, powerup_image
    global e3_image
    if objtype == "HERO":
        return hero_image
    if objtype == "PICKUP":
        return pickup_image 
    elif objtype == "ENEMY":
        return e2_image
    elif objtype == "ENEMY1":
        return e1_image
    elif objtype == "ENEMY2":
        return e3_image
    elif objtype.startswith("POWER"):
        return powerup_image

def loadData():
    global map_image, hero_image, e1_image, e2_image, e3_image
    global smoke_image, pickup_image, rect_image, powerup_image
    global bgrnd_sound, pick_sound, crash_sound
    map_image = pyglet.image.load(filepath('map.png')).get_texture()
    hero_image = pyglet.image.load(filepath('hero.png')).get_texture()
    e1_image = pyglet.image.load(filepath('e1.png')).get_texture()
    e2_image = pyglet.image.load(filepath('e2.png')).get_texture()
    e3_image = pyglet.image.load(filepath('e3.png')).get_texture()
    smoke_image = pyglet.image.load(filepath('smoke.png')).get_texture()
    pickup_image = pyglet.image.load(filepath('pickup.png')).get_texture()
    rect_image = pyglet.image.load(filepath('rect.png')).get_texture()       
    powerup_image = pyglet.image.load(filepath('powerup.png')).get_texture()       

    bgrnd_sound = pyglet.media.load(filepath('bg.wav'))
    pick_sound = pyglet.media.load(filepath('pick.wav'), streaming=False)
    crash_sound = pyglet.media.load(filepath('crash.wav'), streaming=False)
